<template>
    <div>
        <div style="margin-top: 10px;display: flex;justify-content: center">
            <el-input v-model="keywords" placeholder="通过用户名搜索用户" prefix-icon="el-icon-search"style="width: 400px;margin-right: 10px" @keydown.enter.native="search"></el-input>
            <el-button icon="el-icon-search" @click="search" type="primary"> 搜索</el-button>
            <el-button icon="el-icon-plus" @click="editHrRole('')" type="primary"> 新增</el-button>
        </div>
        <div class="hrcontainer">
            <el-card class="box-card" v-for="(hr,index) in hrs" :key="index" >
                <div slot="header" class="clearfix">
                    <span>{{hr.name}}</span>
                    <el-button style="float: right; padding: 3px 0;color: #ff0000" type="text" icon="el-icon-delete" @click="deleteHrRole(hr)"></el-button>
                    <el-button style="float: right; padding: 3px 0;color: #409EFF;margin-left: 5px" type="text" icon="el-icon-edit" @click="editHrRole(hr)"></el-button>
                </div>
                <div>
                    <div class="img-container">
                        <img :src="hr.userface" :alt="hr.name" :title="hr.name" class="userface-img">
                    </div>
                    <div class="userinfo-container">
                        <div>用 户 名：{{hr.name}}</div>
                        <div>手机号码：{{hr.phone}}</div>
                        <div>电话号码：{{hr.telephone}}</div>
                        <div>地 址：{{hr.address}}</div>
                        <div>用户状态：
                            <el-switch
                                    @change="enableChange(hr)"
                                    v-model="hr.enabled"
                                    active-text="启用"
                                    inactive-text="禁用">
                            </el-switch>
                        </div>
                        <div>
                            用户角色：
                            <el-tag type="success" style="margin-right: 4px" size="small" v-for="(role,index) in hr.roles" :key="index">{{role.nameZh}}</el-tag>
                            <el-popover
                                    @show="doRole(hr)"
                                    @hide="hideRole(hr)"
                                    placement="right"
                                    title="角色列表"
                                    width="200"
                                    trigger="click"
                            >
                                <el-select v-model="selectrid" multiple placeholder="请选择">
                                    <el-option
                                            v-for="(r,indexR) in allRoles"
                                            :key="indexR"
                                            :label="r.nameZh"
                                            :value="r.id">
                                    </el-option>
                                </el-select>
                                <el-button slot="reference" icon="el-icon-more" type="text"></el-button>
                            </el-popover>
                        </div>
                        <div>备   注：{{hr.remark}}</div>
                    </div>
                </div>
            </el-card>
        </div>
        <!-- 弹窗, 新增 / 修改 -->
        <AddOrUpdate v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="innitHr"></AddOrUpdate>
    </div>
</template>

<script>
    import AddOrUpdate from '../../components/sys/hr/addOrUpdateHr'
    export default {
        name: "SysHr",
        data() {
            return{
                addOrUpdateVisible: false,
                keywords:'',
                hrs:[],
                allRoles:[],
                selectrid:[]
            }
        },
        components: {
            AddOrUpdate
        },
        mounted() {
            this.innitHr();
        },

        methods:{
            // 新增或编辑按钮
            editHrRole(hr){
                this.addOrUpdateVisible = true;
                var id = '';
                if (hr) {
                    id = hr.id;
                }
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                });
            },
            //删除hr
            deleteHrRole(hr) {
                this.$confirm('此操作将永久删除该【'+hr.name+'】角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/hr/"+hr.id).then(resp =>{
                        if (resp) {
                            this.innitHr();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //展示下拉框时
            doRole(hr) {
                // this.selectrid =[];
                // let rol = hr.roles;
                // rol.forEach(r=>{
                //     this.selectrid.push(r.id);
                // })
                // this.initAllRole();
            },
            //查询所有roles
            initAllRole() {
                // this.getRequest("/system/hr/role").then(resp =>{
                //     if (resp) {
                //         this.allRoles = resp;
                //     }
                // })
            },
            hideRole(hr) {
                // let flag = false;
                //
                // let rolesnew = [];
                // //重新拷贝一个新list
                // Object.assign(rolesnew,hr.roles);
                // if (rolesnew.length != this.selectrid.length) {
                //     flag = true;
                // } else {
                //     for (let i=0;i<rolesnew.length;i++){
                //         let id = rolesnew[i].id;
                //         for (let j = 0; j < this.selectrid.length; j++) {
                //             let selectridElement = this.selectrid[j];
                //             if (id == selectridElement) {
                //                 rolesnew.splice(i,1);
                //                 i--;
                //                 break;
                //             }
                //         }
                //     }
                //     if (rolesnew.length != 0) {
                //         flag = true;
                //     }
                // }
                // if (flag) {
                //     let url = "/system/hr/updatrole?hid="+hr.id;
                //     this.selectrid.forEach(roleid =>{
                //         url +="&rid="+roleid;
                //     });
                //     this.putRequest(url).then(resp =>{
                //         if (resp) {
                //             this.innitHr();
                //         }
                //     });
                // }
            },
            //点击查询
            search() {
                this.innitHr();
            },
            enableChange(hr) {
                // delete hr.roles;
                // delete hr.accountNonExpired;
                // delete hr.accountNonLocked;
                // delete hr.authorities;
                // delete hr.credentialsNonExpired;
                // console.log(hr);
                // this.updateHr(hr);
            },
            updateHr(hr) {
                this.putRequest("/system/hr/",hr).then(resp =>{
                    if (resp) {
                        this.innitHr();
                    }
                })
            },
            innitHr() {
                this.getRequest("/system/hr/?name="+this.keywords).then(resp =>{
                    if (resp) {
                        this.hrs = resp;
                    }
                })
            }
        }
    }
</script>

<style scoped>
    .hrcontainer {
        margin-top: 10px;

        display: flex;

        flex-wrap: wrap;

        justify-content: space-between;
    }
    .box-card {
        width: 400px;
        margin-bottom: 20px;
    }
    .userface-img {
        width: 72px;
        height: 72px;
        /*圆形*/
        border-radius: 72px;
    }
    .img-container {
        width: 100%;
        display: flex;
        justify-content: center;
    }
    .userinfo-container {
        margin-top: 20px;

    }
    .userinfo-container div {
        font-size: 12px;
        color: #409eff;
        margin-top: 4px;
    }
</style>
